#include "consolewidget.h"
#include "ui_consolewidget.h"

#include "keyboard.h"
#include "monitor.h"

ConsoleWidget::ConsoleWidget(QWidget *parent) :
    QWidget(parent), ui(new Ui::ConsoleWidget),
    keyboard(KeyBoard::getInstance()), monitor(Monitor::getInstance()),
    keyboardReady(true)
{
    ui->setupUi(this);
    connect(keyboard, SIGNAL(keyBoardAvailable()), this, SLOT(acceptInput()));
    connect(monitor, SIGNAL(letterAvailable(QChar)), this, SLOT(showLetter(QChar)));
}

ConsoleWidget::~ConsoleWidget()
{
    delete ui;
}

void ConsoleWidget::acceptInput()
{
    keyboardReady = true;
}

void ConsoleWidget::showLetter(QChar letter)
{
    ui->plainTextEdit->insertPlainText(QString(letter));
    monitor->setDSR();
}

void ConsoleWidget::keyPressEvent(QKeyEvent *event)
{
    QWidget::keyPressEvent(event);
    if(keyboardReady)
    {
        QString key = event->text();
        //ui->plainTextEdit->insertPlainText(key);
        keyboard->inputChar(key.at(0));
        keyboardReady = false;
    }
}
